package com.junyidao.game.service;

/**
 * Created by zzh on 2016/8/24.
 */

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.mapper.TMsgSendingMapper;
import com.junyidao.game.pojo.TGroup;
import com.junyidao.game.pojo.TMsgSending;
import com.junyidao.game.vo.MsgSendingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MsgSendingService {

    @Autowired
    private TMsgSendingMapper msgSendingMapper;

    @Autowired
    private SequenceService sequenceService;

    public Map<String, Object> getMsgSendDingOrSave(MsgSendingVo vo) {
        PageHelper.startPage(vo.getPage(), vo.getRows());
        List<MsgSendingVo> list = msgSendingMapper.getMsgSendDingOrSave(vo);
        PageInfo pageInfo = new PageInfo(list);
        Map<String, Object> ret = new HashMap<>();
        ret.put("list", list);
        ret.put("count", pageInfo.getTotal());
        return ret;
    }

    public List<MsgSendingVo> getMsgSending(String ids) {
        List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
        MsgSendingVo vo = new MsgSendingVo();
        vo.setIds(idList);
        List<MsgSendingVo> list = msgSendingMapper.getMsgSendDingOrSave(vo);
        return list;
    }

    public void delSending(String ids) {
        List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", idList);
        msgSendingMapper.delMsgSending(map);
    }

    public void addSendingMsg(TMsgSending msg) {
        Integer id = sequenceService.getNextSequence("MSG_ID");
        msg.setMsgId(id);
        msg.setCreateUid(LoginInfo.getUser().getUid());
        msg.setSendTime(new Date());
        msgSendingMapper.insert(msg);
    }

}
